package controllers

import (
	"GoLangHelloWorld/models"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// UserController 结构体
type UserController struct{}

// GetUsers 获取所有用户
func (uc *UserController) GetUsers(c *gin.Context) {
	users := []models.User{
		{ID: 1, Username: "Alice", Email: "alice@example.com"},
		{ID: 2, Username: "Bob", Email: "bob@example.com"},
	}
	c.JSON(http.StatusOK, users)
}

// GetUser 获取单个用户
func (uc *UserController) GetUser(c *gin.Context) {
	id := c.Param("id")
	fmt.Println(id)
	user := models.User{ID: 1, Username: "Alice", Email: "alice@example.com"} // 示例数据
	c.JSON(http.StatusOK, user)
}

// CreateUser 创建用户
func (uc *UserController) CreateUser(c *gin.Context) {
	var user models.User
	if err := c.ShouldBindJSON(&user); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusCreated, user)
}

// UpdateUser 更新用户
func (uc *UserController) UpdateUser(c *gin.Context) {
	var user models.User
	if err := c.ShouldBindJSON(&user); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, user)
}

// DeleteUser 删除用户
func (uc *UserController) DeleteUser(c *gin.Context) {
	id := c.Param("id")
	c.JSON(http.StatusOK, gin.H{"id": id})
}
